// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include "CombatUnit.h"
#include "GameManager.h"
#include "SpecialEffects.h"

using namespace std;

CombatUnit::CombatUnit(void)
	: _shield(0), _lastHealed(0)
{
}

CombatUnit::~CombatUnit(void)
{
}

double CombatUnit::getShield() const
{
	return _shield;
}

//operations
void CombatUnit::attack(CombatUnit* combatUnit, float timestep, float totalTime)
{
	combatUnit->receiveDamage(maxFirepower() * timestep, this, totalTime);
}


// Receive a certain amount of damage
// param damager: the unit that inflicts the damage.
void CombatUnit::receiveDamage(double damage, CombatUnit* damager, float totalTime)
{
	_shield -= damage;
}

void CombatUnit::heal(float totalTime)
{	
	double shield = _shield;
	int _maxShield = maxShield();

	if(shield< 0.2*_maxShield) {
		shield += 0.3*_maxShield; //30% healing rate
	}
	else if(shield>0.8*_maxShield && shield<_maxShield) {
		shield += 0.1*_maxShield; //10% healing rate
	}
	else if(shield<_maxShield) {
		shield += 0.2*_maxShield; //20% healing rate
	}

	_shield = (shield>_maxShield)? _maxShield: (int)shield;

	// Stop smoking effect if necessary
	if (_shield > 0.33 * maxShield()) {
		GameManager* gm = GameManager::getInstance();
		gm->getSfxManager().resetEffect(this, SFXT_SMOKE);
		gm->getSfxManager().startEffect(this, SFXT_SMOKE_OFF, totalTime);
	}
}

void CombatUnit::update(float timestep, float totalTime) {
	if((totalTime-_lastHealed)>DAY_IN_SECONDS) {
		heal(totalTime); //heal once per day
		_lastHealed = totalTime;
	}

	SpaceObject::update(timestep, totalTime);
}